VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "ApiPrinterDriver"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

'\\ --[ApiPrinterDriver]------------------------------------------------
'\\ Class wrapper for the Printer Driver related API structures
'\\ and calls
'\\ ----------------------------------------------------------------------------------------
'\\ (c) 2001 - Merrion Computing.  All rights  to use, reproduce or publish this code reserved
'\\ Please check http://www.merrioncomputing.com for updates.
'\\ ----------------------------------------------------------------------------------------
'\\ Sourced from: winspool.h

Public Enum PrinterDriverOSVersion
     DRIVER_OS_WIN9x = &H0
     DRIVER_OS_NT351 = &H1
     DRIVER_OS_NT4 = &H2
     DRIVER_OS_WIN2K = &H3
End Enum

Private Type DRIVER_INFO_1
    lpDriverName As String
End Type

Private Type DRIVER_INFO_2
     lVersion As PrinterDriverOSVersion
     lpDriverName As String
     lpEnvironment As String
     lpDriverPath As String
     lpDataFile As String
     lpConfigFile As String
End Type

Private Type DRIVER_INFO_3
     lVersion As PrinterDriverOSVersion
     lpDriverName As String
     lpEnvironment As String
     lpDriverPath As String
     lpDataFile As String
     lpConfigFile As String
     lpHelpFile As String
     lpDependentFiles As String
     lpMonitorName As String
     lpDefaultDataType As String
End Type

'\\ The following types are only supported by drivers
'\\ written for Win2K and WinXP
Private Type DRIVER_INFO_4
     lVersion As PrinterDriverOSVersion
     lpDriverName As String
     lpEnvironment As String
     lpDriverPath As String
     lpDataFile As String
     lpConfigFile As String
     lpHelpFile As String
     lpDependentFiles As String
     lpMonitorName As String
     lpDefaultDataType As String
     lpCompatiblePreviousNames As String
End Type


'\\ API Functions used...
Private Declare Function GetPrinterDriver Lib "winspool.drv" Alias "GetPrinterDriverA" _
                  (ByVal hPrinter As Long, _
                   ByVal lpEnvironment As String, _
                   ByVal Level As Long, _
                   pDriverBuffer As Long, _
                   ByVal cBufferSize As Long, _
                   cBufferSizeRequired As Long) As Long
                   
Private Declare Function EnumPrinterDrivers Lib "winspool.drv" Alias "EnumPrinterDriversA" _
                  (ByVal lpServerName As String, _
                   ByVal lpEnvironment As String, _
                   ByVal Level As Long, _
                   lpDriverBuffer As Long, _
                   ByVal cBufferSize As Long, _
                   cBufferSizeRequired As Long) As Long
                   
'\\ Private member variables....
Private mhPrinter As Long
Private mEnvironment As String

Private mDRIVER_INFO_1 As DRIVER_INFO_1
Private mDRIVER_INFO_2 As DRIVER_INFO_2
Private mDRIVER_INFO_3 As DRIVER_INFO_3
Private mDRIVER_INFO_4 As DRIVER_INFO_4

Private Sub RefreshDriverInfo(ByVal index As Integer)

Dim lRet As Long
Dim SizeNeeded As Long

Dim buffer() As Long

'\\ Find out how much buffer space we're going to need
ReDim Preserve buffer(0 To 1) As Long
lRet = GetPrinterDriver(mhPrinter, mEnvironment, index, buffer(0), UBound(buffer), SizeNeeded)
'\\ Resize the buffer to accomodate that amount
ReDim Preserve buffer(0 To (SizeNeeded / 4) + 3) As Long
lRet = GetPrinterDriver(mhPrinter, mEnvironment, index, buffer(0), UBound(buffer) * 4, SizeNeeded)

If index < 1 Or index > 4 Then
    '\\ Index is out of bounds - notify developer of error
    Debug.Print "Error in call to ApiPrinterDriver:RefreshPrinterDriverInfo - invalid index"
Else
    Select Case index
    Case 1
        With mDRIVER_INFO_1
            .lpDriverName = StringFromPointer(buffer(0), 1024)
        End With
    Case 2
        With mDRIVER_INFO_2
            .lVersion = buffer(0)
            .lpDriverName = StringFromPointer(buffer(1), 1024)
            .lpEnvironment = StringFromPointer(buffer(2), 1024)
            .lpDriverPath = StringFromPointer(buffer(3), 1024)
            .lpDataFile = StringFromPointer(buffer(4), 1024)
            .lpConfigFile = StringFromPointer(buffer(5), 1024)
        End With
    Case 3
        With mDRIVER_INFO_3
            .lVersion = buffer(0)
            .lpDriverName = StringFromPointer(buffer(1), 1024)
            .lpEnvironment = StringFromPointer(buffer(2), 1024)
            .lpDriverPath = StringFromPointer(buffer(3), 1024)
            .lpDataFile = StringFromPointer(buffer(4), 1024)
            .lpConfigFile = StringFromPointer(buffer(5), 1024)
            .lpHelpFile = StringFromPointer(buffer(6), 1024)
            .lpDependentFiles = StringFromPointer(buffer(7), 1024)
            .lpMonitorName = StringFromPointer(buffer(8), 1024)
            .lpDefaultDataType = StringFromPointer(buffer(9), 1024)
        End With
    Case 4
        With mDRIVER_INFO_4
            .lVersion = buffer(0)
            .lpDriverName = StringFromPointer(buffer(1), 1024)
            .lpEnvironment = StringFromPointer(buffer(2), 1024)
            .lpDriverPath = StringFromPointer(buffer(3), 1024)
            .lpDataFile = StringFromPointer(buffer(4), 1024)
            .lpConfigFile = StringFromPointer(buffer(5), 1024)
            .lpHelpFile = StringFromPointer(buffer(6), 1024)
            .lpDependentFiles = StringFromPointer(buffer(7), 1024)
            .lpMonitorName = StringFromPointer(buffer(8), 1024)
            .lpDefaultDataType = StringFromPointer(buffer(9), 1024)
            .lpCompatiblePreviousNames = StringFromPointer(buffer(10), 1024)
        End With
    End Select
End If
End Sub
